package com.example.demo.controller.service;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.controller.BaseController;
import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.service.Service;
import com.example.demo.domain.tos.Tos;
import com.example.demo.service.service.ServiceService;
import com.example.demo.web.TableDataInfo;


/***
 * @author zby
 */
@Controller
@RequestMapping("/service")
public class ServiceController extends BaseController {
    @Autowired
    private ServiceService serviceService;


    @RequestMapping("/")
    public String index() {

        return "index";
    }

    //加载服务管理的页面
    @GetMapping("")
    public String service() {
        return "service/service";
    }

    //查询方法，并将结果渲染到表格中
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Service service) {
        startPage();
        List<Service> list = serviceService.selectServiceList(service);
        return getDataTable(list);
    }


    @GetMapping("/add")
    public String add() {
        return "service" + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(Service service) {
        return toAjax(serviceService.insert(service));
    }

    /**
     * 修改
     */
    //从前端获取选中的信息
    @GetMapping("/edit/{serviceId}")
    public String edit(@PathVariable("serviceId") Integer serviceId, ModelMap mmap) {
        Service service = serviceService.selectByPrimaryKey(serviceId);
        mmap.put("service", service);
        return "service/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Service service) {

        return toAjax(serviceService.updateByPrimaryKey(service));
    }

    /**
     * 删除广告
     */

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        String[] id = ids.split(",");
        return toAjax(serviceService.deleteServiceByIds(id));
    }

    //查询服务类型名称
    @PostMapping("/selectTosName")
    @ResponseBody
    public List<Tos> selectTosName() {

        List<Tos> tos = serviceService.selectTosName();

        return tos;
    }

}
